package com.ruoyi.app.service.impl;

/**
 * @author wqg
 * @description 处理全额还款试算
 * @date 2022/6/10
 */

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.app.service.ITrialService;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.service.ILoanIouService;
import com.ruoyi.business.service.IDoRepaymentLastService;
import com.ruoyi.business.service.IRepaymentService;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service("repayAllService")
@Slf4j
public class RepayAllService implements ITrialService {
    @Autowired
    IRepaymentService repaymentService;
    @Autowired
    IDoRepaymentLastService doRepaymentLastService;
    @Autowired
    ILoanIouService loanIouService;

    @Override
    public JSONObject handle(JSONObject params) {
        LoanIou loanIou = loanIouService.getLoanIouByLoanSerialNo(params.getString("loanSerialNo"));
        BigDecimal iouRepayAmt = LoanIou.calculationOverdueAmount(loanIou);
        log.info("repayAll还款试算：{},{}", loanIou.getLoanSerialNo(), iouRepayAmt);
        // 进一步验证
        if (params.getBoolean("isJustWallet")) {
            log.info("repayAll还款试算:{},只用钱包还款", loanIou.getLoanSerialNo());
            // 接口查询钱包是够足够,此处不冻结钱包
            BigDecimal amtWalletObj = doRepaymentLastService.getWalletAmount(loanIou.getCustNo());
            if (amtWalletObj.compareTo(iouRepayAmt) < 0) {
                log.info("repayAll还款试算:{},只用钱包还款钱包不够", loanIou.getLoanSerialNo());
                throw new BusinessException(RspEnmu.REPAYMENT_WALLET_NOT_ENOUGH);
            }
        }
        return repaymentService.repaymentTrial(params,loanIou);
    }
}
